package com.zhilehuo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhilehuo.common.BaseResponse;
import com.zhilehuo.mapper.ParagraphMapper;
import com.zhilehuo.model.po.Paragraph;
import com.zhilehuo.model.vo.ParagraphVO;
import com.zhilehuo.service.IParagraphService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ParagraphService implements IParagraphService {

    @Resource
    private ParagraphMapper paragraphMapper;

    @Override
    public PageInfo<ParagraphVO> getParagraphsByArticleId(Integer articleID, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<Paragraph> paragraphs = paragraphMapper.getParagraphsByArticleId(articleID);

        List<ParagraphVO> paragraphVOList = new ArrayList<>();

        for (Paragraph paragraph : paragraphs) {
            ParagraphVO paragraphVO = new ParagraphVO();

            paragraphVO.setId(paragraph.getId());
            paragraphVO.setArticleId(paragraph.getArticleId());
            paragraphVO.setFont(paragraph.getFont());
            paragraphVO.setContent(paragraph.getContent());
            paragraphVO.setParagraphOrder(paragraph.getParagraphOrder());
            paragraphVO.setImageUrl(paragraph.getImageUrl());

            paragraphVOList.add(paragraphVO);
        }


        return new PageInfo<>(paragraphVOList);
    }

    @Override
    public void updateByPrimaryKeySelective(Paragraph paragraph) {
        paragraphMapper.updateByPrimaryKeySelective(paragraph);
    }

    @Override
    public BaseResponse<String> updateFont(Integer font, Integer paragraphId) {

        int flag = paragraphMapper.updateFont(font, paragraphId);
        if (flag == 1) {
            return new BaseResponse<>(200, "修改成功");
        }
        return new BaseResponse<>(400, "修改异常");

    }
}
